package com.oauth.facebook.client.ui.tabs;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.facebook.client.beans.FacebookUser;
import com.oauth.facebook.client.rpc.FacebookRemoteServiceAsync;
import com.oauth.facebook.client.ui.ConnectionPanel;

public class MyConnectionsTab extends Composite {

	private static MyConnectionsTabUiBinder uiBinder = GWT.create(MyConnectionsTabUiBinder.class);

	interface MyConnectionsTabUiBinder extends UiBinder<Widget, MyConnectionsTab> {
	}

	@UiField
	VerticalPanel connectionsPanel;

	@UiField
	DeckPanel deckPanel;

	public MyConnectionsTab() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void init() {
		deckPanel.showWidget(0);
		connectionsPanel.clear();

		FacebookRemoteServiceAsync.INSTANCE
				.getConnections(new OAuthAsnyCallBack<List<FacebookUser>>() {

					@Override
					public void onSuccess(List<FacebookUser> pResult) {
						if (pResult.isEmpty()) {
							connectionsPanel.add(new Label("You don't have any connections"));
						} else {
							for (FacebookUser user : pResult) {
								connectionsPanel.add(new ConnectionPanel(user));
							}
						}
						deckPanel.showWidget(1);
					}
				});
	}
}
